package com.tool4j.tools;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.tool4j.config.ApplicationContextProvider;
import com.tool4j.entity.TableMetadata;
import com.tool4j.entity.params.JavaCodeConfig;
import com.tool4j.entity.vo.FileResult;
import com.tool4j.util.GenerateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 代码生成工具
 */
@Component
@Slf4j
public class GenerateCodeTool {

    /**
     * 基于表结构生成CRUD代码
     *
     * @param dataList 表结构数组
     * @return
     */
    @Tool(name = "generateCrudByTable", description = "基于表结构生成CRUD代码", returnDirect = true)
    public static String generateCode(List<TableMetadata> dataList, Map<String, Object> config) {
        GenerateCodeUtil generateCodeUtil = ApplicationContextProvider.getBean("generateCodeUtil");
        JavaCodeConfig javaCodeConfig = BeanUtil.copyProperties(config, JavaCodeConfig.class);
        List<FileResult> result = generateCodeUtil.generate(dataList, javaCodeConfig);
        log.info("代码生成成功");
        return JSON.toJSONString(result);
    }

}
